﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model;

namespace ShivamEMR.Web.Models
{
    public class CustomAuthorizeAttribute : AuthorizeAttribute
    {

        public string RequiredRight;
        public int CustomValue;
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            //return true;
            //return base.AuthorizeCore(httpContext);
             var usr=  Registry.RepositoryFactory.GetUserRepository().All.Where(u => u.Name == httpContext.User.Identity.Name).SingleOrDefault();
          if (usr != null)
          {
              if (usr.Role.Rights.Where(r => r.Title == RequiredRight).Count() > 0)
                  return true;
              if (usr.Rights.Where(r => r.Title == RequiredRight).Count() > 0)
                  return true;
          }
          return false;
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
        }
    }
}